<?php
/*
 * Copyright (C) 2014 University of Pittsburgh
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once('util/Constants.inc');
require_once('database/ReferenceObject.inc');
require_once('database/DocumentDAO.inc');

if(isset($_POST['valid_request_key']) && $_POST['valid_request_key'] === Constants::VALID_REQUEST_KEY) {
  
  $is_keyword_search_type_OR = true;
  $keyword_arr = array ();
  $jurisdiction_arr = array ();
  $acting_agent_arr = array ();
  $aa_subset_arr = array ();
  $aa_footnote_arr = array ();
  $participating_agent_arr = array ();
  $pa_subset_arr = array ();
  $pa_footnote_arr = array ();
  $emergency_arr = array ();
  $timeframe_arr = array ();
  $action_arr = array ();
  $goal_arr = array ();
  $prescription_arr = array ();
  $purpose_arr = array ();
  $condition_arr = array ();
  
  if(isset($_POST['txt_keyword']) && trim($_POST['txt_keyword']) !== '') {
    $keyword_arr = explode(',', $_POST['txt_keyword']);
    // Trim each element
    $keyword_arr = array_map('trim', $keyword_arr);
  }
  
  if(isset($_POST['keyword_search_type'])) {
    $is_keyword_search_type_OR = $_POST['keyword_search_type'];
  }
  
  if(isset($_POST['slct_jurisdiction'])) {
    $jurisdiction_arr = $_POST['slct_jurisdiction'];
  }
  
  if(isset($_POST['slct_acting_agent'])) {
    $acting_agent_arr = $_POST['slct_acting_agent'];
  }
  
  if(isset($_POST['slct_aa_subset'])) {
    $aa_subset_arr = $_POST['slct_aa_subset'];
  }
  
  if(isset($_POST['slct_aa_footnote'])) {
    $aa_footnote_arr = $_POST['slct_aa_footnote'];
  }
  
  if(isset($_POST['slct_participating_agent'])) {
    $participating_agent_arr = $_POST['slct_participating_agent'];
  }
  
  if(isset($_POST['slct_pa_subset'])) {
    $pa_subset_arr = $_POST['slct_pa_subset'];
  }
  
  if(isset($_POST['slct_pa_footnote'])) {
    $pa_footnote_arr = $_POST['slct_pa_footnote'];
  }
  
  if(isset($_POST['slct_emergency'])) {
    $emergency_arr = $_POST['slct_emergency'];
  }
  
  if(isset($_POST['slct_timeframe'])) {
    $timeframe_arr = $_POST['slct_timeframe'];
  }
  
  if(isset($_POST['slct_action'])) {
    $action_arr = $_POST['slct_action'];
  }
  
  if(isset($_POST['slct_goal'])) {
    $goal_arr = $_POST['slct_goal'];
  }
  
  if(isset($_POST['slct_prescription'])) {
    $prescription_arr = $_POST['slct_prescription'];
  }
  
  if(isset($_POST['slct_purpose'])) {
    $purpose_arr = $_POST['slct_purpose'];
  }
  
  if(isset($_POST['slct_condition'])) {
    $condition_arr = $_POST['slct_condition'];
  }
  
  $ref_obj = new ReferenceObject($keyword_arr, $jurisdiction_arr, $acting_agent_arr, $aa_subset_arr, $aa_footnote_arr, $participating_agent_arr, $pa_subset_arr, $pa_footnote_arr, $emergency_arr, $timeframe_arr, $action_arr, $goal_arr, $prescription_arr, $purpose_arr, $condition_arr);
  
  $sql_statement = $ref_obj->get_doc_sec_inv_sql_statement();
  
  $docDAO = new DocumentDAO();
  $is_error = false;
  /*
   * Now that we have the SELECT statement, execute the query
   */
  $doc_sec_id_arr = $docDAO->select_doc_sec_ids($sql_statement);
  $document_id_arr = NULL;
  $document_arr = NULL;
  
  if(!is_null($doc_sec_id_arr)) {
    if(count($doc_sec_id_arr) > 0) {
      $sql_statement = $ref_obj->get_document_inv_sql_statement($doc_sec_id_arr);
      $document_id_arr = $docDAO->select_document_ids($sql_statement);
      if(!is_null($document_id_arr)) {
        if(count($document_id_arr) > 0) {  
          //If there are keywords, perform a keyword search
          if(count($keyword_arr) > 0) {
            $sql_statement = $ref_obj->get_document_inv_keyword_sql_statement($document_id_arr, $is_keyword_search_type_OR);
            $document_id_arr = $docDAO->select_document_ids($sql_statement);
            if(is_null($document_id_arr)) {
              $is_error = true;
            }
          }
          
          if(!$is_error) {
            if(count($document_id_arr) > Constants::QUERY_LIMIT) {
              for($i = 0; $i < Constants::QUERY_LIMIT; ++$i) {
                $document = $docDAO->find_document($document_id_arr[$i]);
                if(is_null($document)) {
                  continue;
                } else {
                  $document_arr[] = $document;
                }
              }
            } else {    
              foreach($document_id_arr as $document_id) {
                $document = $docDAO->find_document($document_id);
                if(is_null($document)) {
                  continue;
                } else {
                  $document_arr[] = $document;
                }
              }
            }
          }
        }
      } else {
        //Database Error
        $is_error = true;
      }
    }
  } else {
    //Database Error
    $is_error = true;  
  }

  if($is_error == true) {
    //There was a Database Error, so return it
    $temp_arr['phasys_lena_search']['document_data'][0] = Constants::DATA_NONE;
    $temp_arr['phasys_lena_search']['error_data']['error_code'] = $docDAO->error_code();
    $temp_arr['phasys_lena_search']['error_data']['error_message'] = $docDAO->error_info();
  } else {
    if(count($document_arr) > 0) {     
      $temp_arr['phasys_lena_search']['document_data'] = $document_arr;
      $temp_arr['phasys_lena_search']['document_id_data'] = $document_id_arr;
      $temp_arr['phasys_lena_search']['error_data']['error_code'] = Constants::ERR_CODE_NONE;
      $temp_arr['phasys_lena_search']['error_data']['error_message'] = Constants::ERR_MSG_NONE;
    } else {
      //There was no error, but the search returned 0 Rows
      $temp_arr['phasys_lena_search']['document_data'][0] = Constants::DATA_NONE;
      $temp_arr['phasys_lena_search']['error_data']['error_code'] = Constants::ERR_CODE_NONE;
      $temp_arr['phasys_lena_search']['error_data']['error_message'] = Constants::ERR_MSG_NONE;
    }
  }
  
  echo(json_encode($temp_arr));

} else {
  //INVALID REQUEST KEY
  $temp_arr['phasys_lena_search']['document_data'][0] = Constants::DATA_NONE;
  $temp_arr['phasys_lena_search']['error_data']['error_code'] = Constants::ERR_CODE_REQUEST_KEY_REQUIRED;
  $temp_arr['phasys_lena_search']['error_data']['error_message'] = Constants::ERR_MSG_REQUEST_KEY_REQUIRED;
  
  echo(json_encode($temp_arr));
  //echo '{"phasys_lena_search":{"search_results_data":"' . Constants::DATA_NONE . '", "phasys_lena_error_data":{"error_code":"' . Constants::ERR_CODE_REQUEST_KEY_REQUIRED . '", "error_message":"' . Constants::ERR_MSG_REQUEST_KEY_REQUIRED . '"}}}';
}


?>